﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SerializedObject.cs" company="Marcelo Roca">
//   Copyright (c) 2011 Marcelo Roca. All rights reserved.
// </copyright>
// <author name="Marcelo Roca" email="marce@marceloroca.com"/>
// <license>
//   This program is free software: you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
// 
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program.  If not, see http://www.gnu.org/licenses/.
// </license>
// --------------------------------------------------------------------------------------------------------------------

namespace GameKits.Unity.SmartInspector.Editor.Serializer
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;

    using UnityEngine;

    /// <summary>
    /// El objeto serializado
    /// </summary>
    internal class SerializedObject
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SerializedObject"/> class.
        /// </summary>
        /// <param name="target">
        /// The target.
        /// </param>
        public SerializedObject(MonoBehaviour target)
        {
            // buscamos todos las propiedades del taget
            var t = target.GetType();

            this.FieldList = (from info in t.GetMembers(BindingFlags.Public | BindingFlags.Instance)
                              let attr =
                                  info.GetCustomAttributes(true).OfType<BrowsableFieldAttribute>().FirstOrDefault()
                              where
                                  attr != null &&
                                  (info.MemberType == MemberTypes.Property || info.MemberType == MemberTypes.Field || info.MemberType == MemberTypes.Method)
                              select
                                  new SerializedItem(info, target, attr)).ToList();

            this.MethodList = (from info in t.GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Static)
                               let attr =
                                   info.GetCustomAttributes(true).OfType<BrowsableMethodAttribute>().FirstOrDefault()
                               where
                                   attr != null && ((MethodInfo)info).GetParameters().Length == 0
                               select
                                   new SerializedItem(info, target, attr)).ToList();
        }

        /// <summary>
        /// Gets FieldList.
        /// </summary>
        public List<SerializedItem> FieldList { get; private set; }

        /// <summary>
        /// Gets MethodList.
        /// </summary>
        public List<SerializedItem> MethodList { get; private set; }
    }
}
